// pages/collect/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    collect:[],
    curIndex:-1,
    tabs: [{
            id: 0,
            name: "商品收藏",
            isActive: "true",
          },
          {
            id: 1,
            name: "店铺收藏",
            isActive: "false",
          },
          {
            id: 2,
            name: "我的足迹",
            isActive: false,
          },
          {
            id: 3,
            name: "红包卡券",
            isActive: false,
          }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let collect = wx.getStorageSync('collect') || [];
    this.setData({
      collect
    })
  },

  // async loadCollect(){

  //   wx.setStorageSync('collect', collect);
  //   this.setData({
  //     collect
  //   })
  // },

  changeByIndex(index){
    let tabs = JSON.parse(JSON.stringify(this.data.tabs));
    tabs.forEach((v,i)=>i === index ? v.isActive=true:v.isActive=false);
    this.setData({
      tabs
    })
  },

  handleClick(e){
    // console.log(e);
    const {index} = e.detail;
    // console.log(index);
    this.changeByIndex(index);
    this.setData({
      curIndex:index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

   /*
    问题：
    */
  onShow: function () {
    // this.loadCollect();
   const collect=wx.getStorageSync('collect')||[];
  
    let currentPages =  getCurrentPages();
    // console.log(currentPages);
    let currentPage = currentPages[currentPages.length - 1];
    // console.log(currentPage.options);
    const {type} = currentPage.options;
     const index = parseInt(type);
    this.changeByIndex(index);
   this.setData({
     collect,
     curIndex:index
   })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})